home *** CD-ROM | disk | FTP | other *** search
/ 9-Digit Zip Code Directory / 9-Digit Zip Code Directory (American Business Information) (ABIZIP-12).ISO / z4src.zip / Z4INQ.CPP < prev    next >
C/C++ Source or Header  |  1995-09-20  |  13KB  |  493 lines

  1. //----------------------------------------------------------------------------
  2. //                            MODULE DESCRIPTION
  3. //
  4. //  Module:    z4inq.cpp
  5. //   Title:    ZIP+4 Engine
  6. //  Notice:    John M. Weeder
  7. //                 Copyright (c) 1993. All rights reserved.
  8. //             This module contains proprietary information and should be 
  9. //                treated as confidential.
  10. //
  11. //----------------------------------------------------------------------------
  12. //                           MAINTENANCE HISTORY
  13. //
  14. // $Workfile$
  15. // $Revision$
  16. //   $Author$
  17. //     $Date$
  18. //      $Log$    
  19. //
  20. //----------------------------------------------------------------------------
  21. //                             MODULE NARRATIVE
  22. //
  23. //    This module contains code for the class Z4_INQ.
  24. //
  25. //    The code in this module may be written in C++ or C.
  26. //
  27. //    This module is portable to:
  28. //        DOS 3.X+
  29. //        MS Windows 3.X+
  30. //        OS/2 2.X+
  31. //        OS/2 2.0 PM
  32. //
  33. //    The following compilers are supported:
  34. //        MSC 6.0A
  35. //        MSC/C++ 7.0
  36. //        Borland C++ 3.1 for DOS
  37. //        Borland C++ 1.0 for OS/2 2.X
  38. //
  39. //----------------------------------------------------------------------------
  40. #include <z4.h>
  41.  
  42.  
  43. //----------------------------------------------------------------------------
  44. //    Globals
  45. //----------------------------------------------------------------------------
  46. BOOL Z4_INQ::fInit = FALSE;
  47.  
  48. Z4_AB_FILE Z4_INQ::ab_file;
  49. Z4_CS_FILE Z4_INQ::cs_file;
  50. Z4_CX_FILE Z4_INQ::cx_file;
  51. Z4_ST_FILE Z4_INQ::st_file;
  52. Z4_Z4_FILE Z4_INQ::z4_file;                    
  53. Z4_Z5_FILE Z4_INQ::z5_file;
  54. Z4_ZX_FILE Z4_INQ::zx_file;
  55.  
  56. Z4_CS Z4_INQ::cs;                                    
  57. Z4_CX Z4_INQ::cx;                                    
  58. Z4_Z4 Z4_INQ::z4;
  59. Z4_Z5 Z4_INQ::z5;
  60. Z4_ZX Z4_INQ::zx;
  61.  
  62.  
  63. //----------------------------------------------------------------------------
  64. //   Description:    Default constructor
  65. //    Parameters:
  66. //       Returns:    
  67. //----------------------------------------------------------------------------
  68. FN_M Z4_INQ::Z4_INQ()
  69. {
  70.     Z4_INQ::Initialize(CL_INIT_CLASS);
  71. }
  72.  
  73.  
  74. //----------------------------------------------------------------------------
  75. //   Description:    Copy constructor
  76. //    Parameters:    rcz4_inq        Reference to object to copy.
  77. //       Returns:    
  78. //----------------------------------------------------------------------------
  79. FN_M Z4_INQ::Z4_INQ(RCZ4_INQ rcz4_inq)
  80. {
  81.     Z4_INQ::Initialize(CL_INIT_CLASS);
  82.     *this = rcz4_inq;
  83. }
  84.  
  85.  
  86. //----------------------------------------------------------------------------
  87. //   Description:    Destructor
  88. //    Parameters:
  89. //       Returns:
  90. //----------------------------------------------------------------------------
  91. FN_M Z4_INQ::~Z4_INQ()
  92. {
  93.     Z4_INQ::Destroy(FALSE);
  94. }
  95.  
  96.  
  97. //----------------------------------------------------------------------------
  98. //   Description:
  99. //    Parameters:
  100. //       Returns:    TRUE if successful.
  101. //----------------------------------------------------------------------------
  102. VOID FN_M Z4_INQ::Clear()
  103. {
  104.     request = Z4_RQ_DFT;
  105.     result = Z4_RESULT_FAILURE;
  106.  
  107.     fInvZip4 = FALSE;
  108.     fInvZip5 = FALSE;
  109.     fInvCity = FALSE;
  110.     fInvState = FALSE;
  111.     fZip5Changed = FALSE;
  112.     fCSChanged = FALSE;
  113.     fAddrChanged = FALSE;
  114.     fFacilityOther = FALSE;
  115.     fNonDeliverable = FALSE;
  116.     fBetterAvailable = FALSE;
  117.     fNoPrimary = FALSE;
  118.     fSecAvail = FALSE;
  119.     fInvSec = FALSE;
  120.     fStackOverFlow = FALSE;
  121.                     
  122.     szAddr1[0] = '\0';
  123.     szAddr2[0] = '\0';
  124.     szCity[0] = '\0';
  125.     szState[0] = '\0';
  126.     szZip[0] = '\0';
  127.                     
  128.     match.Clear();
  129.     parse.Clear();
  130.     std.Clear();
  131.     cSelect = 0;
  132.     return ;
  133. }
  134.  
  135.  
  136. //----------------------------------------------------------------------------
  137. //   Description:    
  138. //    Parameters:
  139. //       Returns:    TRUE if successful.
  140. //----------------------------------------------------------------------------
  141. BOOL FN_M Z4_INQ::Close()
  142. {
  143.     ab_file.Destroy();
  144.     cs_file.Destroy();
  145.     cx_file.Destroy();
  146.     st_file.Destroy();
  147.     z4_file.Destroy();
  148.     z5_file.Destroy();
  149.     zx_file.Destroy();
  150.     return TRUE;
  151. }
  152.  
  153.  
  154. //----------------------------------------------------------------------------
  155. //   Description:    Destroy object. Free any resources used by object.
  156. //                          Normally called by destructor.
  157. //                        Should allow multiple calls from various classes.
  158. //                        A class should almost always re-init its variables when 
  159. //                        it is destroyed to prevent accidents.
  160. //    Parameters:    fDestroyAll        Destroy parents also?
  161. //                                            Default is TRUE.
  162. //       Returns:    TRUE if successful.
  163. //----------------------------------------------------------------------------
  164. BOOL FN_M Z4_INQ::Destroy(BOOL fDestroyAll)
  165. {
  166.     Z4_INQ::Initialize(CL_INIT_CLASS_VARS);
  167.     if (fDestroyAll)                            // Destroy parent.
  168.         Z4_INQ_PARENT::Destroy(fDestroyAll);
  169.     return TRUE;
  170. }
  171.  
  172.  
  173. //----------------------------------------------------------------------------
  174. //   Description:
  175. //    Parameters:
  176. //       Returns:    TRUE if successful.
  177. //----------------------------------------------------------------------------
  178. VOID FN_M Z4_INQ::Flags()
  179. {
  180.     fStackOverFlow   = match.fStackOverFlow;
  181.     fBetterAvailable = match.fBetterAvailable;
  182.     fInvZip4         = match.fInvZip4;
  183.     fInvZip5         = match.fInvZip5;
  184.     fInvCity         = match.fInvCity;
  185.     fNoPrimary          = match.fNoPrimary;
  186.     fSecAvail          = match.fSecAvail;
  187.     fInvSec              = match.fInvSec;
  188.  
  189.     fInvState = parse.fInvState;
  190.     
  191.    fFacilityOther    = std.fFacilityOther;                        
  192.     fNonDeliverable = std.fNonDeliverable;
  193.  
  194.     fAddrChanged = stricmp(szAddr1, std.szAddr1) != 0 || stricmp(szAddr2, std.szAddr2) != 0;
  195.     fCSChanged = stricmp(parse.szCity, std.szCity) != 0 || parse.state != std.state;
  196.     fZip5Changed = stricmp(parse.szZip5, std.szZip5) != 0;
  197.  
  198.     return ;
  199. }
  200.  
  201.  
  202. //----------------------------------------------------------------------------
  203. //   Description:    Initialize object. 
  204. //                          Normally called by constructor.
  205. //                        Should allow multiple calls from various classes.
  206. //    Parameters:    sInit        Initialization code. May be one of the following:
  207. //                                        CL_INIT_CLASS            Reset class variables and
  208. //                                                                    and dynamic allocations for
  209. //                                                                    this class only.
  210. //                                        CL_INIT_CLASS_VARS    Reset class variables for
  211. //                                                                    this class only.
  212. //                                        CL_INIT_VARS            Reset class variables for
  213. //                                                                    this class only.
  214. //                                        CL_INIT_ALL                Initialize class and all 
  215. //                                                                    parent class, including
  216. //                                                                    dynamic memory allocation.
  217. //                                    Default is CL_INIT_ALL
  218. //       Returns:    TRUE if successful.
  219. //----------------------------------------------------------------------------
  220. BOOL FN_M Z4_INQ::Initialize(SHORT sInit)
  221. {
  222.     if (sInit == CL_INIT_VARS || sInit == CL_INIT_ALL)
  223.         Z4_INQ_PARENT::Initialize(sInit);
  224.  
  225.     pfnzip4 = NULL;
  226.     return TRUE;
  227. }
  228.  
  229.  
  230. //----------------------------------------------------------------------------
  231. //   Description:    
  232. //    Parameters:
  233. //       Returns:    TRUE if successful.
  234. //----------------------------------------------------------------------------
  235. BOOL FN_M Z4_INQ::Inquiry(Z4_RQ _request)
  236. {
  237.     request = _request;
  238.     result = Z4_RESULT_FAILURE;
  239.     match.pfnzip4 = pfnzip4;                // Propagate call back function!
  240.  
  241.     strtrim(szAddr1);
  242.     strtrim(szAddr2);
  243.     strtrim(szCity);
  244.     strtrim(szState);
  245.     strtrim(szZip);
  246.  
  247.       if (pfnzip4 != NULL && !pfnzip4())
  248.         {
  249.         result = Z4_RESULT_ABORT;
  250.         goto EXIT_FUNC;
  251.         }
  252.     parse.Clear();
  253.     if (!parse.LastLine(szZip, szCity, szState))
  254.         goto EXIT_FUNC;
  255.  
  256.     if (request == Z4_RQ_DFT)
  257.         {
  258.         if (szAddr1[0] || szAddr2[0])
  259.             request = Z4_RQ_Z4;
  260.         else if (parse.szZip4[0])
  261.             request = Z4_RQ_ZX;
  262.         else if (parse.szZip5[0])
  263.             request = Z4_RQ_Z5;
  264.         else if (parse.szCity[0])
  265.             request = Z4_RQ_CS;
  266.         else
  267.             {
  268.             result = Z4_RESULT_INV_CSZ;
  269.               if (pfnzip4 != NULL)
  270.                 pfnzip4(1);
  271.             goto EXIT_FUNC;
  272.             }
  273.         }
  274.     switch (request)
  275.         {
  276.         case Z4_RQ_CS:
  277.             result = match.CS(parse);
  278.             break;
  279.  
  280.         case Z4_RQ_Z5:
  281.             result = match.Z5(parse);
  282.             break;
  283.  
  284.         case Z4_RQ_Z4:
  285.             if (!parse.Primary(szAddr2)
  286.             || !parse.Secondary(szAddr1))
  287.                 break;
  288.             result = match.Z4(parse);
  289.             break;
  290.  
  291.         case Z4_RQ_ZX:
  292.             result = match.ZX(parse);
  293.             break;
  294.  
  295.         default:
  296.             break;
  297.         }
  298.     if (result != Z4_RESULT_SUCCESS
  299.     && result != Z4_RESULT_EXACT
  300.     && result != Z4_RESULT_MULTI)
  301.         {
  302.         match.cRecords = 0;
  303.         }
  304.     cSelect = 0;
  305.     if (match.cRecords && result != Z4_RESULT_FAILURE)
  306.         Select(cSelect);
  307.  
  308. EXIT_FUNC:
  309.       if (pfnzip4 != NULL)
  310.         pfnzip4(1);
  311.     return TRUE;
  312. }
  313.  
  314.  
  315. //----------------------------------------------------------------------------
  316. //   Description:    
  317. //    Parameters:
  318. //       Returns:    TRUE if successful.
  319. //----------------------------------------------------------------------------
  320. BOOL FN_M Z4_INQ::Open(FLAG16 fs)
  321. {
  322.     if (fs & Z4_INQ_AB)
  323.         {
  324.         Z4_INQ::ab_file.ClearError();
  325.         if (!Z4_INQ::ab_file.Open())
  326.             return FALSE;
  327.         }
  328.     if (fs & Z4_INQ_CX)
  329.         {
  330.         Z4_INQ::cx_file.ClearError();
  331.         if (!Z4_INQ::cx_file.Open())
  332.             return FALSE;
  333.         }
  334.     if (fs & Z4_INQ_CS)
  335.         {
  336.         Z4_INQ::cs_file.ClearError();
  337.         if (!Z4_INQ::cs_file.Open())
  338.             return FALSE;
  339.         }
  340.     if (fs & Z4_INQ_ST)
  341.         {
  342.         Z4_INQ::st_file.ClearError();
  343.         if (!Z4_INQ::st_file.Open())
  344.             return FALSE;
  345.         }
  346.     if (fs & Z4_INQ_Z4)
  347.         {
  348.         Z4_INQ::z4_file.ClearError();
  349.         if (!Z4_INQ::z4_file.Open())
  350.             return FALSE;
  351.         }
  352.     if (fs & Z4_INQ_Z5)
  353.         {
  354.         Z4_INQ::z5_file.ClearError();
  355.         if (!Z4_INQ::z5_file.Open())
  356.             return FALSE;
  357.         }
  358.     if (fs & Z4_INQ_ZX)
  359.         {
  360.         Z4_INQ::zx_file.ClearError();
  361.         if (!Z4_INQ::zx_file.Open())
  362.             return FALSE;
  363.         }
  364.     return TRUE;
  365. }
  366.  
  367.  
  368. //----------------------------------------------------------------------------
  369. //   Description:    
  370. //    Parameters:
  371. //       Returns:    TRUE if successful.
  372. //----------------------------------------------------------------------------
  373. BOOL FN_M Z4_INQ::Select(SIZET _cSelect)
  374. {
  375.     cSelect = MIN(match.cRecords, _cSelect);
  376.     std.Clear();
  377.     switch (request)
  378.         {
  379.         case Z4_RQ_CS:
  380.             {
  381.             CHAR szZip5[MAX_ZIP5+1];
  382.             szZip5[MAX_ZIP5] = '\0';
  383.             strb2a(cx.abZip5[cSelect], MAX_ZIP5_BCD, szZip5, MAX_ZIP5, TRUE);
  384.             if (!std.CS(cx, szZip5))
  385.                 {
  386.                 result = Z4_RESULT_FAILURE;
  387.                 return FALSE;
  388.                 }
  389.             break;
  390.             }
  391.  
  392.         case Z4_RQ_Z4:
  393.             if (!std.Z4(match.arecid[cSelect], parse))
  394.                 {
  395.                 result = Z4_RESULT_FAILURE;
  396.                 return FALSE;
  397.                 }
  398.             break;
  399.  
  400.         case Z4_RQ_Z5:
  401.             if (!std.Z5(match.arecid[cSelect], parse.szZip5))
  402.                 {
  403.                 result = Z4_RESULT_FAILURE;
  404.                 return FALSE;
  405.                 }
  406.             break;
  407.  
  408.         case Z4_RQ_ZX:
  409.             if (!std.ZX(match.arecid[cSelect], parse.szAddon))
  410.                 {
  411.                 result = Z4_RESULT_FAILURE;
  412.                 return FALSE;
  413.                 }
  414.             break;
  415.  
  416.         default:
  417.             result = Z4_RESULT_FAILURE;
  418.             return FALSE;
  419.         }
  420.     Flags();
  421.     return TRUE;
  422. }
  423.  
  424.  
  425. //----------------------------------------------------------------------------
  426. //   Description:
  427. //    Parameters:
  428. //       Returns:    TRUE if successful.
  429. //----------------------------------------------------------------------------
  430. BOOL FN_M Z4_INQ::Set(PCSZ pcszCity, PCSZ pcszState, PCSZ pcszZip, PCSZ pcszAddr1, PCSZ pcszAddr2)
  431. {
  432.     Clear();
  433.     strtrim(strcpy(szAddr1, (pcszAddr1 ? pcszAddr1: "")));
  434.     strtrim(strcpy(szAddr2, (pcszAddr2 ? pcszAddr2: "")));
  435.     strtrim(strcpy(szCity,  (pcszCity  ? pcszCity:  "")));
  436.     strtrim(strcpy(szState, (pcszState ? pcszState: "")));
  437.     strtrim(strcpy(szZip,   (pcszZip   ? pcszZip:   "")));
  438.     return TRUE;
  439. }
  440.  
  441.  
  442. //----------------------------------------------------------------------------
  443. //   Description:    Initialize engine
  444. //    Parameters:    
  445. //       Returns:    TRUE if successful.
  446. //----------------------------------------------------------------------------
  447. BOOL FN_E Z4_INQ::Start(FLAG16 fs)
  448. {
  449.     BOOL fWrite = BTEST(fs, Z4_INQ_WRITE);
  450.     BOOL fReinit = BTEST(fs, Z4_INQ_REINIT);
  451.  
  452.     if (fInit)                                    // Only re-init if forced
  453.         {
  454.         if (fReinit)                            // If re-initing, terminate first
  455.             Terminate();
  456.         else
  457.             return TRUE;
  458.         }
  459.     CL_PARM cl_parm;                            // Get data path from configuration file
  460.     cl_parm.SetName("DATA");
  461.     cl_parm.Get();
  462.     PCSZ pcsz = (PCSZ)cl_parm;
  463.     if (pcsz == NULL || !pcsz[0])            // If none found, check environment
  464.         pcsz = EnvGet("DATA");
  465.     DioSetDataPath(pcsz);                    // Set data path
  466.     fInit = TRUE;                                // Open files
  467.     if (!DioOpenAllPhysical(CfgGet(CFG_PROGRAM_ID), fWrite)
  468.         || !Z4_INQ::Open(fs)) {
  469.                 return FALSE;                         }
  470.  
  471.     DioRelease();                                // Release directory and header caches!
  472.     return TRUE;
  473. }
  474.  
  475.  
  476. //----------------------------------------------------------------------------
  477. //   Description:    Terminate engine
  478. //    Parameters:    
  479. //       Returns:    TRUE if successful.
  480. //----------------------------------------------------------------------------
  481. BOOL FN_E Z4_INQ::Terminate()
  482. {
  483.     if (!fInit)
  484.         return TRUE;
  485.     fInit = FALSE;
  486.     Z4_INQ::Close();                            // Close data files
  487.     DioCloseAll();                                // Close physical files
  488.     return TRUE;
  489. }
  490. //----------------------------------------------------------------------------
  491. //------------------------------- End of File --------------------------------
  492. //----------------------------------------------------------------------------
  493.